package com.ruoyi.quartz.task;

import com.ruoyi.api.ThirdPartyApi;
import com.ruoyi.api.factory.ThirdPartyApiFactory;
import com.ruoyi.api.resp.ApiResponse;
import com.ruoyi.config.NTFYTopicEnum;
import com.ruoyi.entity.MessageRequest;
import com.ruoyi.entity.WeatherData;
import com.ruoyi.feign.NtfyServiceClient;
import com.ruoyi.service.GdCityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component("weatherTask")
@Slf4j
public class WeatherTask {
    @Autowired
    private ThirdPartyApiFactory thirdPartyApiFactory;

    @Autowired
    NtfyServiceClient ntfyServiceClient;

    @Autowired
    GdCityService gdCityService;
    public void weatherTask(String cityName) {
        ThirdPartyApi gdWeatherApi = thirdPartyApiFactory.createGdWeatherApi();
        List<String> cityNameList = Arrays.asList(cityName.split(","));
        cityNameList.stream().forEach(
                city -> {
                    String cityCodeByName = gdCityService.getCityCodeByName(city);
                    ApiResponse<WeatherData> apiResponse = gdWeatherApi.getWeatherData(cityCodeByName);
                    WeatherData data = apiResponse.getData();
                    if(data.getWeather().contains("雨") || data.getWeather().contains("雪")){
                        String content = " 今天 " + city + " 天气:"+data.getWeather()+"，记得带伞!";
                        MessageRequest message = MessageRequest.builder().
                                topic(NTFYTopicEnum.WEATHER.getCode()).
                                title("记得带伞通知").
                                type("ntfy").
                                content(content).build();
                        ntfyServiceClient.sendNtfyMessage(message);
                    }else {
                        String content = " 今天 " + city + " 天气无雨/雪:"+data.getWeather()+"!";
                        MessageRequest message = MessageRequest.builder().
                                topic(NTFYTopicEnum.WEATHER.getCode()).
                                title("不用带伞通知！").
                                type("ntfy").
                                content(content).build();
                        ntfyServiceClient.sendNtfyMessage(message);
                    }



                    log.info("data:{}",data );
                    log.info("任务执行完毕！");
                }
        );


    }
}
